package DateUtils

import (
	"time"
	"errors"
	"strconv"
)

func GetCurrDate()(string){
	return time.Now().Format("20060102")
}
func GetCurrMonth()(string){
	return time.Now().Format("200601")
}
func GetCurrYear()(string){
	return time.Now().Format("2006")
}
func GetCurrTime()(string){
	return time.Now().Format("150405")
}
func GetCurrDateTime()(string){
	return time.Now().Format("20060102150405")
}
func GetDateDiff(date1 string,date2 string)(int64,error){
	var d1,err1 = time.Parse("20060102",date1)
	var d2,err2 = time.Parse("20060102",date2)
	if(err1!=nil||err2!=nil){
		return 0,errors.New("日期输入异常")
	}
	var d1_d = d1.Unix()/(60*60*24)
	var d2_d = d2.Unix()/(60*60*24)
	var diff = d1_d-d2_d
	return diff,nil
}
func GetDateTimeDiff(date1 string,date2 string)(int64,error){
	var d1,err1 = time.Parse("20060102150405",date1)
	var d2,err2 = time.Parse("20060102150405",date2)
	if(err1!=nil||err2!=nil){
		return 0,errors.New("日期输入异常")
	}
	var d1_d = d1.Unix()
	var d2_d = d2.Unix()
	var diff = d1_d-d2_d
	return diff,nil
}
func GetCountDateTime(datetime string,second int64)(string,error){
	var d1,err1 = time.Parse("20060102150405",datetime)
	if(err1!=nil){
		return datetime,errors.New("日期输入异常")
	}
	var d1_d = d1.Unix()
	d1_d = d1_d + second
	var str = time.Unix(d1_d,0).Format("20060102150405")
	return str,nil
}
func GetCountDate(date string,day int64)(string,error){
	var d1,err1 = time.Parse("20060102",date)
	if(err1!=nil){
		return date,errors.New("日期输入异常")
	}
	var d1_d = d1.Unix()
	d1_d = d1_d + day*60*60*24
	var str = time.Unix(d1_d,0).Format("20060102")
	return str,nil
}
func GetCountMonth(month string,m int64)(string,error){
	var m1,err1 = time.Parse("200601",month)
	if(err1!=nil){
		return month,errors.New("日期输入异常")
	}
	var m1_m = m1.AddDate(0,int(m),0)
	var str = m1_m.Format("200601")
	return str,nil
}
func GetCountYear(year string,y int64)(string,error){
	var y1,err1 = time.Parse("2006",year)
	if(err1!=nil){
		return year,errors.New("日期输入异常")
	}
	var y1_m = y1.AddDate(int(y),0,0)
	var str = y1_m.Format("2006")
	return str,nil
}
func IsLeapYear(year string)(bool,error){
	var y_int,err = strconv.Atoi(year)
	if(err!=nil){
		return false,err
	}
	var result = (y_int % 4 == 0 && y_int % 100 != 0) || (y_int % 400 == 0)
	return result,nil
}
func GetMonthDay(year int,month int)(int){
	var numDays = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	var n int = numDays[month - 1];
	if (month == 2) {
		isLeapYear,err := IsLeapYear(strconv.Itoa(year))
		if(err!=nil){
			return -1
		}
		if(isLeapYear){
			n=29
		}else{
			n=28
		}
	}
	return n
}
func FirstDayOfMonth(today string)(string){
	var d,err = time.Parse("20060102",today)
	if(err!=nil){
		return today
	}
	d = time.Date(d.Year(),d.Month(),1,0,0,0,0,time.Local)

	return d.Format("20060102")
}
func LastDayOfMonth(today string)(string){
	var d,err = time.Parse("20060102",today)
	if(err!=nil){
		return today
	}
	var month ,err2 = strconv.Atoi(d.Format("01"))
	if(err2!=nil){
		return today
	}
	var d_last = GetMonthDay(d.Year(),month)
	d = time.Date(d.Year(),d.Month(),d_last,0,0,0,0,time.Local)

	return d.Format("20060102")
}
func GetDateBetween(date1 string,date2 string)(int64,error){
	var d1,err1 = time.Parse("20060102",date1)
	var d2,err2 = time.Parse("20060102",date2)
	if(err1!=nil||err2!=nil){
		return 0,errors.New("日期输入异常")
	}
	var d1_d = d1.Unix()/(60*60*24)
	var d2_d = d2.Unix()/(60*60*24)
	var diff = d1_d-d2_d
	if(diff<0){
		diff = -diff
	}
	return diff,nil
}
func GetDateTimeBetween(date1 string,date2 string)(int64,error){
	var d1,err1 = time.Parse("20060102150405",date1)
	var d2,err2 = time.Parse("20060102150405",date2)
	if(err1!=nil||err2!=nil){
		return 0,errors.New("日期输入异常")
	}
	var d1_d = d1.Unix()
	var d2_d = d2.Unix()
	var diff = d1_d-d2_d
	if(diff<0){
		diff = -diff
	}
	return diff,nil
}
func GetMonthBetween(month1 string,month2 string)(int64,error){
	var m1,err1 = time.Parse("200601",month1)
	var m2,err2 = time.Parse("200601",month2)
	if(err1!=nil||err2!=nil){
		return 0,errors.New("日期输入异常")
	}
	var m1_m,_ = strconv.Atoi(m1.Format("01"))
	var m2_m,_ =  strconv.Atoi(m2.Format("01"))
	var diff = m1_m-m2_m
	if(diff<0){
		diff = -diff
	}
	return int64(diff),nil
}
func GetNextDate(date string)(string,error){
	return GetCountDate(date,1)
}
func GetLastDate(date string)(string,error){
	return GetCountDate(date,-1)
}
func GetCurrDateByFormat(format string)(string){
	return time.Now().Format(format)
}
func GetCurrDateHm()(string) {
	return time.Now().Format("2006-01-02 15:04:05")
}
